{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "86ba6b70",
   "metadata": {},
   "outputs": [],
   "source": [
    "import collections\n",
    "import copy\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "810db206",
   "metadata": {},
   "outputs": [],
   "source": [
    "metadata = []\n",
    "del_data = []\n",
    "key_dict = collections.defaultdict(int)\n",
    "length = 0\n",
    "confidence_num = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "283043a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "87442\n"
     ]
    }
   ],
   "source": [
    "# question 1\n",
    "with open('gaze.csv', newline='') as csv_file:\n",
    "    title = csv_file.readline().split(',')\n",
    "title[-1] = title[-1][: 14]\n",
    "for i in range(len(title)):\n",
    "    key_dict[title[i]] = i\n",
    "with open('gaze.csv', newline='') as csv_file:\n",
    "    reader = csv.DictReader(csv_file)\n",
    "    for row in reader:\n",
    "        length += 1\n",
    "        tem = []\n",
    "        for i in title:\n",
    "            tem.append((row[i]))\n",
    "        metadata.append(tem)\n",
    "        if float(row['confidence']) >= 0.9:\n",
    "            confidence_num += 1\n",
    "            del_data.append(tem)\n",
    "print(confidence_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9282168a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "87377\n"
     ]
    }
   ],
   "source": [
    "# question 2\n",
    "mean_x, mean_y = 0, 0\n",
    "sigma = 0\n",
    "fit_num = 0\n",
    "x, y = key_dict['norm_pos_x'], key_dict['norm_pos_y']\n",
    "for i in del_data:\n",
    "    mean_x += float(i[x])\n",
    "    mean_y += float(i[y])\n",
    "mean_x /= confidence_num\n",
    "mean_y /= confidence_num\n",
    "mean = (mean_x ** 2 + mean_y ** 2) ** (1 / 2)\n",
    "for i in del_data:\n",
    "    sigma += (float(i[x]) - mean_x) ** 2 + (float(i[y]) - mean_y) ** 2\n",
    "sigma = (sigma / (confidence_num - 1)) ** (1 / 2)\n",
    "limit_min, limit_max = mean - 3 * sigma, mean + 3 * sigma\n",
    "while fit_num < len(del_data):\n",
    "    if limit_min < (float(del_data[fit_num][x]) ** 2 + float(del_data[fit_num][y]) ** 2) ** (1 / 2) < limit_max:\n",
    "        fit_num += 1\n",
    "    else:\n",
    "        del_data.pop(fit_num)\n",
    "print(fit_num)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "654cd8ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1970-01-03T0:29:10.825638499998604+0000\n"
     ]
    }
   ],
   "source": [
    "# question 3\n",
    "changed_data = copy.deepcopy(del_data)\n",
    "for i in range(len(del_data)):\n",
    "    x = key_dict['gaze_timestamp']\n",
    "    k = float(del_data[i][x])\n",
    "    k1 = k % 86400\n",
    "    time_stack = [int(k // 86400)]\n",
    "    k2 = k1 % 3600\n",
    "    time_stack.append(int(k1 // 3600))\n",
    "    time_stack.append(int(k2 // 60))\n",
    "    k3 = k2 % 60\n",
    "    time_stack.append(k3)\n",
    "    changed_data[i][x] = '1970-01-0' + str(2 + time_stack[0]) + 'T' + ':'.join(\n",
    "        [str(i) for i in time_stack[1:]]) + '+0000'\n",
    "print(changed_data[0][0])     # 测试输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0975ca8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "427HZ\n"
     ]
    }
   ],
   "source": [
    "# question 4\n",
    "print(str(int(length // (float(metadata[-1][0]) - float(metadata[0][0])))) + 'HZ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "50a42f73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['88150.77678', '0', '0.8280059462620806', '0.5314327126985153', '0.39750946211500204', '88150.77678-0', '28.369752901573236', '52.033134560158175', '705.1211159195539', '20.0', '15.0', '-20.0', '0.01152676874215287', '0.051001789765111646', '0.9986320398640939', '', '', '', '', '', '']\n",
      "['88150.787485', '1', '0.5025948800258426', '0.3401089118572799', '0.14298949506249015', '88150.787485-1', '-139.4048601388959', '175.088321362755', '681.1514528333081', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.13698933743054134', '0.2204754503935745', '0.9657248558487515', '', '', '', '', '', '']\n",
      "['88150.7935835', '1', '0.6251340345888996', '0.4959927312055917', '0.39902068406553226', '88150.79569-0 88150.79147699999-1', '1.5330999491406576', '-21.730861601028412', '-298.89044052583546', '20.0', '15.0', '-20.0', '0.06514516047431963', '0.027374145920454677', '0.9975002577452808', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14508725538827758', '0.23074439357001145', '0.9621365356122304']\n",
      "['88150.80114', '1', '0.7084760742125342', '0.49514541550942315', '0.40833847319653394', '88150.801157-0 88150.801123-1', '1.7824437847763495', '-18.930992853762366', '-286.84930905199883', '20.0', '15.0', '-20.0', '0.06722208090235383', '0.026625007974036028', '0.9973827253314249', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1525226416003532', '0.22265933259715345', '0.9628913050841411']\n",
      "['88150.81287299999', '2', '0.8563407607235008', '0.49480891967227525', '0.40212548107788093', '88150.809749-0 88150.815997-1', '1.9143205570488036', '-20.302479515303915', '-288.10244511278336', '20.0', '15.0', '-20.0', '0.066337739085217', '0.02383990799003523', '0.9975123874719999', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.15209387168484892', '0.23398036211890086', '0.960271130638957']\n",
      "['88150.821819', '2', '0.861112802026488', '0.49345299232127626', '0.3998385355256633', '88150.823534-0 88150.820104-1', '2.4518232083297207', '-21.099370287407083', '-292.5744058209181', '20.0', '15.0', '-20.0', '0.06331981940889361', '0.025199443737810433', '0.9976750916532551', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1515273956574198', '0.23414301276977342', '0.9603210389949675']\n",
      "['88150.83163849999', '2', '0.7924554336009598', '0.49356274160390745', '0.3980066812275601', '88150.831679-0 88150.83159799999-1', '2.429747589010555', '-21.65481450143157', '-294.88334739937204', '20.0', '15.0', '-20.0', '0.06287006068157094', '0.02741066830429456', '0.9976452329024624', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1502104716193903', '0.23374654728633698', '0.9606244666088807']\n",
      "['88150.842234', '2', '0.861800715322522', '0.4927738756185537', '0.3994444195460858', '88150.841019-0 88150.84344899999-1', '2.7401863260151327', '-21.448818102327536', '-296.25432032362136', '20.0', '15.0', '-20.0', '0.061462251502523456', '0.024974843062789222', '0.9977968975970163', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1505621238051983', '0.23344803725625876', '0.960642004482663']\n",
      "['88150.851535', '3', '0.8146346350814423', '0.49287349446746376', '0.3989298236709611', '88150.85153599999-0 88150.851534-1', '2.7137850087172026', '-21.64932512225967', '-297.50128283500806', '20.0', '15.0', '-20.0', '0.061283019104364955', '0.02566851258753678', '0.9977903181686012', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14981667522740064', '0.23303596604807042', '0.9608585756248681']\n",
      "['88150.8620355', '3', '0.8652883409541539', '0.4928189656783549', '0.39798609028889587', '88150.864506-0 88150.85956499999-1', '2.7479117978994125', '-21.95824899137679', '-298.9549967249147', '20.0', '15.0', '-20.0', '0.06083737507249493', '0.02545753653609314', '0.9978229941366369', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14915185946327889', '0.2340532138000838', '0.9607147422250263']\n",
      "['88150.87172649999', '3', '0.7797779563318543', '0.4922528401501992', '0.39846979262451043', '88150.87172699999-0 88150.871726-1', '2.9698239727970126', '-21.893036021055767', '-299.48716997330905', '20.0', '15.0', '-20.0', '0.05994958006002151', '0.025324603360997427', '0.997880109189093', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14964541784424926', '0.23325783877395026', '0.9608314261975022']\n",
      "['88150.881738', '4', '0.94500047108518', '0.49231789502851864', '0.3989757058831216', '88150.879586-0 88150.88389-1', '2.9448756619455043', '-21.783872023277713', '-299.48615899364256', '20.0', '15.0', '-20.0', '0.06004036186469085', '0.024936294324659092', '0.9978844302686117', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14956825585505618', '0.2328956455844725', '0.9609312957273628']\n",
      "['88150.8917005', '4', '0.923014300448957', '0.4917446414950056', '0.3979021415944922', '88150.891699-0 88150.891702-1', '3.1948566002857905', '-22.225656140802766', '-302.3468596140583', '20.0', '15.0', '-20.0', '0.05856004760502819', '0.024550784512162607', '0.9979819536466253', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14891582435890977', '0.23373323204977325', '0.9608292530367055']\n",
      "['88150.90154349999', '4', '0.8211025221142706', '0.49161345948598506', '0.3974947453981874', '88150.89951199999-0 88150.90357499999-1', '3.2402324612913436', '-22.277270315812324', '-301.84455749704443', '20.0', '15.0', '-20.0', '0.058503550699411246', '0.0250527830279053', '0.9979727915219527', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14932225425095516', '0.23403121968543314', '0.9606936309760579']\n",
      "['88150.91138549999', '5', '0.9291327620219251', '0.4913631044179708', '0.398148615464501', '88150.91139099999-0 88150.91137999999-1', '3.339172825717702', '-22.14984503440082', '-302.04472721887953', '20.0', '15.0', '-20.0', '0.05812076850806207', '0.02427722241941762', '0.9980143249170481', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14955939074696273', '0.23374756422057885', '0.9607258010797586']\n",
      "['88150.92347549999', '5', '0.8565881774627679', '0.4915351855439489', '0.39616555072269743', '88150.91942399999-0 88150.92752699999-1', '3.2889050938853295', '-22.693312256707145', '-303.54559074370997', '20.0', '15.0', '-20.0', '0.057982052995741815', '0.026013439327870827', '0.9979786483210625', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14860165931375657', '0.2343942415664973', '0.9607168606669007']\n",
      "['88150.9318775', '5', '0.9982023170809342', '0.4911730468744113', '0.39620066431503487', '88150.93622799999-0 88150.92752699999-1', '3.4408900347283', '-22.76025240670143', '-304.54396906658593', '20.0', '15.0', '-20.0', '0.05725573600184664', '0.02556757341574248', '0.9980321036342059', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14860165931375657', '0.2343942415664973', '0.9607168606669007']\n",
      "['88150.94089249999', '5', '0.7394207779056206', '0.49142312016421413', '0.39707533688365215', '88150.940894-0 88150.94089099999-1', '3.3348852897642836', '-22.51093611525771', '-303.7677083637994', '20.0', '15.0', '-20.0', '0.057780027483167365', '0.024714737948574338', '0.9980233715460669', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1486475062957136', '0.23424002017754897', '0.9607473818956195']\n",
      "['88150.9515035', '6', '0.8036163788282745', '0.4914462011441675', '0.3964083710376737', '88150.947523-0 88150.95548399999-1', '3.321166938820271', '-22.6244933057039', '-303.33442657283535', '20.0', '15.0', '-20.0', '0.05791441579586018', '0.025849307221017352', '0.9979868404739706', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.1488208667257802', '0.23427704473976166', '0.9607115154587205']\n",
      "['88150.961489', '6', '0.8391189191753998', '0.49079700947050914', '0.39754933751413335', '88150.963521-0 88150.95945699999-1', '3.5715514659817913', '-22.364811155521387', '-303.1921606196255', '20.0', '15.0', '-20.0', '0.057074997183387884', '0.023794625042328963', '0.9980862991323006', '-39.93492801030824', '14.997919452545707', '-20.075282531198983', '-0.14973291454501486', '0.23464854751065256', '0.9604791062032513']\n"
     ]
    }
   ],
   "source": [
    "# question 5\n",
    "time = int(float(metadata[0][0]) * 100)\n",
    "place_stack = [0]\n",
    "for i in range(1, len(metadata)):\n",
    "    if int(float(metadata[i][0]) * 100) == time + 1:\n",
    "        time += 1\n",
    "        place_stack.append(i)\n",
    "for i in range(20):\n",
    "    print(metadata[place_stack[i]])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
